<template>
  <div>
    <div class="top">
      <el-form ref="form" :model="form" label-width="80px" inline size="mini">
        <el-form-item v-show="name!=='transfer'">
          <el-select v-model="form.buildingsId" placeholder="所在栋舍" clearable>
            <el-option
              :label="item.name"
              :value="item.id"
              v-for="item in buildList"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-show="name!=='transfer'">
          <el-select v-model="form.herdsPigsCode" placeholder="所在猪群" clearable>
            <el-option
              :label="item.herdsPigsCode"
              :value="item.herdsPigsCode"
              v-for="item in herdsPigsList"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
          <el-form-item v-show="name =='transfer'" >
            <el-select v-model="form.herdsPigsCodeOut" placeholder="转出群" clearable>
              <el-option :label="item.herdsPigsCodeOut" :value="item.herdsPigsCodeOut" v-for="item in tableList" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-show="name =='transfer'" >
            <el-select v-model="form.herdsPigsCodeIn" placeholder="转入群" clearable>
              <el-option :label="item.herdsPigsCodeIn" :value="item.herdsPigsCodeIn" v-for="item in tableList" :key="item.id"></el-option >
            </el-select>
          </el-form-item>
        <el-form-item label="查询时间">
          <el-date-picker
            v-model="time"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click='searchPorkList'>查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableList" style="width: 100%" border>
      <el-table-column
        :prop="item.prop"
        :label="item.label"
        width="width"
        v-for="(item, index) in tableHead"
        :key="index"
        align="center"
      >
      </el-table-column>
      <!-- 无数据 -->
      <template slot="empty">
        <div class="noData">
          <!--  v-show="data.length===0" -->
          <img src="@/assets/images/noData.png" />
          <p>暂无数据</p>
        </div>
      </template>
    </el-table>
    <Pagination :total="total" @pagination="handlePage" :page.sync="form.pageNum"></Pagination>
  </div>
</template>

<script>
import { entryList, tdList, exitList } from "@/api/event";
import { getlist } from "@/api/system-setup/building";
import { pigList } from "@/api/production-management/pork.js";
import { first } from "../constant/constant";
import {
  porkEntryTableHead,
  porkLeaveTableHead,
  porkTransferTableHead,
} from "../constant/constant";
export default {
  name: "first",
   inject: ["name"],
      inheritAttrs: false,
   data() {
    return {
      data: first,
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      buildList:[],
      herdsPigsList:[],
      tableList: [],
      tableHead: [],
      time: [],
      form: {
        buildingsId: null,
        herdsPigsCode: "",
      },
      total:0
    };
  },
  created() {
    if(this.name !=='transfer'){
      this.getBulidList()
       this.getHerdsPigsCodeList();
    }
    switch (this.name) {
      case "entry":
        this.form = {
          entryPigType: 1,
         buildingsId: null,
        herdsPigsCode: null,
          startEntryTime: "", //开始时间
          endEntryTime: "", //结束时间
          pageNum: 1,
          pageSize: 10,
        };
        this.tableHead = porkEntryTableHead;
        break;
      case "leave":
        this.form = {
          exitPigType: 1,
          buildingsId: null,
        herdsPigsCode: "",
          ids: [],
          startExitTime: "",
          endExitTime: "",
          pageNum: 1,
          pageSize: 10,
        };
        this.tableHead = porkLeaveTableHead;
        break;
      case "transfer":
        this.form = {
          tdPigType: 1,
          buildingsId: null,
          herdsPigsCode: "",
          herdsPigsCodeIn:'',
          herdsPigsCodeOut:'',
          ids: [],
          startTdTime: "",
          endTdTime: "",
          pageNum: 1,
          pageSize: 10,
        };
        this.tableHead = porkTransferTableHead;
        break;
    }
    this.getList()
  },
  methods: {
    //栋宿下拉列表
    async getBulidList() {
      let data = {
        buildingsType: "",
        columnMaxNumber: 0,
        sumColumnNumber: 0,
        name: "",
        pageNum: 0,
        pageSize: 10,
        remark: "",
      };
      let res = await getlist(data);
      if (res.code == 200) {
        this.buildList = res.buildingsList.records;
        console.log(this.buildList)
      }
    },
    //猪群下拉框列表数据
     async getHerdsPigsCodeList() {
      if (this.herdsPigsList.length == 0) {
        let res = await pigList({
          buildingsId: null,
          herdsPigsCode: "", //猪群号
          isPresent: "", //是否在场
          pageNum: 1,
          pageSize: 9999,
        });
        if (res.code == 200) {
          this.herdsPigsList = res.data.list;
        }
      }
    },
    async getList(){
       let method = entryList;
      switch (this.name) {
        case "entry":
          method = entryList;
          break;
        case "leave":
          method = exitList;
          break;
        case "transfer":
          method = tdList;
          break;
      }
      let res = await method(this.form)
      if(res.code == 200){
        this.tableList = res.data.list
        this.total = res.data.total
      }
    },
    handlePage(val){
      this.form.pageNum = val.page
      this.form.pageSize = val.limit
      this.getList()
    },
    searchPorkList(){
      this.form.pageNum = 1;
      this.getList();
    }
  },
  watch: {
    time(newVal) {
      if (newVal) {
        switch (this.name) {
          case "entry":
            this.form.startEntryTime = newVal[0];
            this.form.endEntryTime = newVal[1];
            break;
          case "leave":
            this.form.startExitTime = newVal[0];
            this.form.endExitTime = newVal[1];
            break;
          case "transfer":
            this.form.startTdTime = newVal[0];
            this.form.endTdTime = newVal[1];
            break;
        }
      } else {
        this.time = [];
      }
    },
  }
};
</script>

<style></style>
